package com.zego.wawaji.ui.bean.wawa;

import com.zego.wawaji.base.data.BaseData;
import com.zego.wawaji.base.module.ModuleMgr;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 用户娃娃列表
 * Created by Su on 2017/12/1.
 */
public class MyWawaList extends BaseData {
    private List<MyWawa> myWawaList = Collections.synchronizedList(new ArrayList<MyWawa>());

    @Override
    public void parseJson(String jsonStr) {
        JSONArray jsonArray = getJsonArray(jsonStr);
        this.myWawaList = (List<MyWawa>) getBaseDataList(jsonArray, MyWawa.class);
    }

    // =============================== 已申请发货的娃娃 =================================
    public List<MyWawa> getMyWawaList() {
        return myWawaList;
    }


    // =============================== 寄存中的娃娃 =================================
    public List<MyWawa> getMyWawaUntreatedList() {  // 未过期寄存中的娃娃
        List<MyWawa> list = new ArrayList<>();

        for (MyWawa info : myWawaList) {
            Wawa wawa = ModuleMgr.getWawaMgr().getWawa(info.getWwTypeId());
            if (wawa.isMaterial()) {  // 合成材料
                continue;
            }
            long currentTime = ModuleMgr.getAppMgr().getTime();
            long distance = info.getCallbackTime() - currentTime / 1000;

            if (distance > 0) {
                try {
                    list.add(info);
                } catch (Exception e) {
                }
            }
        }
        return list;
    }

    // =============================== 可合成的娃娃 =================================

    /**
     * 获取可合成的娃娃列表
     */
    public List<MyWawa> getSynthWawaList() {
        List<MyWawa> list = new ArrayList<>();

        for (MyWawa info : myWawaList) {
            Wawa wawa = ModuleMgr.getWawaMgr().getWawa(info.getWwTypeId());
            if (wawa.isMaterial()) {  // 合成材料
                list.add(info);
                continue;
            }

            long currentTime = ModuleMgr.getAppMgr().getTime();
            long distance = info.getCallbackTime() - currentTime / 1000;
            if (distance <= 0) continue;  // 娃娃过期

            if (wawa.canSynth()) {
                try {
                    list.add(info);
                } catch (Exception e) {
                }
            }
        }
        return list;
    }

    // =============================== 娃娃碎片 =================================

    /**
     * 获取购买的碎片
     */
    public ArrayList<MyWawa> getMaterialList() {
        ArrayList<MyWawa> list = new ArrayList<>();

        for (MyWawa info : myWawaList) {
            Wawa wawa = ModuleMgr.getWawaMgr().getWawa(info.getWwTypeId());
            if (wawa.isMaterial()) {
                try {
                    list.add(info);
                } catch (Exception e) {
                }
            }
        }
        return list;
    }
}
